#include <iostream>
#include <signal.h>
#include<ctemplate/template.h>
#include "../comm/httplib.h"
#include "oj_control.hpp"

using namespace httplib;
using namespace ns_control;

static Control *ctrl_ptr = nullptr;//
// Created by TNW on 25-4-8.

int main()
{
    Server svr;
    Control ctrl;
    ctrl_ptr=&ctrl;
    svr.Get("/all_questions", [&ctrl](const Request &req, Response &res) {
        std::string html;
        if (ctrl.AllQuestions(&html))
        {
            res.set_content(html, "text/html; charset=utf-8");
        }
        else
        {
            res.status = 500;
            res.set_content("获取题目列表失败", "text/plain; charset=utf-8");
        }
    });
    svr.Get(R"(/questions/(\d+))", [&ctrl](const Request &req, Response &res) {
        std::string html;
        if (ctrl.GetQuestion(req.matches[1], &html))
        {
            res.set_content(html, "text/html; charset=utf-8");
        }
        else
        {
            res.status = 404;
            res.set_content("题目不存在", "text/plain; charset=utf-8");
        }
    });
    svr.Post(R"(/judge/(\d+))", [&ctrl](const Request &req, Response &res) {
        std::string in_json = req.body;
        std::string out_json;
        ctrl.Judge(req.matches[1],in_json, &out_json);
        res.set_content(out_json, "application/json; charset=utf-8");
    });
    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0", 8080);
    return 0;
}
